package Web.service.impl;

import Web.domain.ResponseResult;
import Web.domain.entity.Article;
import Web.domain.entity.Like;
import Web.domain.entity.Praise;
import Web.domain.entity.User;
import Web.mapper.ArticleMapper;
import Web.mapper.PraiseMapper;
import Web.service.ArticleService;
import Web.service.PraiseService;
import Web.vo.RecommendStatusVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PraiseServiceImpl extends ServiceImpl<PraiseMapper, Praise> implements PraiseService {
    @Autowired
    ArticleService articleService;
    @Autowired
    PraiseMapper praiseMapper;

    @Override
    public Integer setPraise(Praise praise) {
        LambdaQueryWrapper<Praise> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Praise::getArticle_id,praise.getArticle_id());
        lambdaQueryWrapper.eq(Praise::getUser_id,praise.getUser_id());
        Praise pp=praiseMapper.selectOne(lambdaQueryWrapper);
//        Article article=articleService.getById(praise.getUser_id());
//        article.setPhoto_id(praise.getArticle_id());
        Integer result=0;
        if (pp==null){
            result=1;
            save(praise);
            return result;
        }
        else{
            result=2;
            removeById(pp.getId());
            return result;
        }
    }

    @Override
    public ResponseResult getPraiseStatus(Praise praise) {
        LambdaQueryWrapper<Praise> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Praise::getArticle_id,praise.getArticle_id());
        lambdaQueryWrapper.eq(Praise::getUser_id,praise.getUser_id());
        Praise pp=praiseMapper.selectOne(lambdaQueryWrapper);
        RecommendStatusVo recommendStatusVo=new RecommendStatusVo();
        if(pp==null){
            recommendStatusVo.setStatus(0);
        }
        else {
            recommendStatusVo.setStatus(1);
        }
        return ResponseResult.okResult(recommendStatusVo);
    }
}
